package com.usian.xxlJob;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.pojo.shop.Shop;
import com.usian.pojo.error.Error;
import com.usian.service.ErrorService;
import com.usian.service.ShopService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class DemoJob {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ErrorService errorService;

    /**
     * 基于上个项目中，运营人员维护商品后，通过xxl-job来实现自动审核
     * 审核通过标准：不能和已上架的商品名称重复
     *              审核时提交的商品库存要小于100
     * */
    @XxlJob("AutomaticAudit")
    public ReturnT<String > hello(String param) throws Exception{
        System.out.println("======================================自动审核开始：");

        //得到存在的所有数据
        List<Shop> shopList = shopService.list(new QueryWrapper<>());
        //循环得到单条数据
        for (Shop shop : shopList) {
            //与上架的商品进行比较，看是否重名
            Integer number = shopService.compareSname(shop);
            //判断state是否为11，y-判断是否符合条件
            if (shop.getState().equals(11)){
                //判断库存
                if (shop.getStock()<100){
                    String errorReason = "库存小于100";

                    //修改
                    shopService.updateById(shop);

                    //审核失败要维护失败原因
                    Error error = new Error(shop.getSid(),errorReason,new Date());
                    errorService.save(error);
                }else if (number>0){
                    //判断是否和 已上架的商品重名
                    String errorReason = "和已上架的商品名称重复";

                    //修改
                    shopService.updateById(shop);

                    //审核失败要维护失败原因
                    Error error = new Error(shop.getSid(),errorReason,new Date());
                    errorService.save(error);
                }else {
                    //审核通过
                    shop.setState(5);
                    //修改
                    shopService.updateById(shop);
                }
            }
        }

        System.out.println("======================================自动审核结束");
        return ReturnT.SUCCESS;
    }
}
